var user_pic_page;
$(function(){

    $('.user_box .set_bg_pic').click(function(){
        $('#ASMJHDGUYASGD .asdasdasdas').click();
    });
    $('#ASMJHDGUYASGD .asdasdasdas').on('change',function(){
        var imgUrl = getObjectURL($(this)[0].files[0]);
        console.log($(this)[0].files[0]);
        if($(this)[0].files[0] > 2048000){
            return false;
        }

        if(!$(this)[0].files[0].type=='image/png'||!$(this)[0].files[0].type=='image/gif'||!$(this)[0].files[0].type=='image/jpeg'){
            return false;
        }

        $('#user_box').css({
            background:'url('+imgUrl+') no-repeat'
        });

        $(this).parents('form').on_submit(function (e) {
            console.log(e)
        });
    });

    $('.user_item .head_pic').click(function(){
        console.log(1)
        $('#HeaderImage .header_img_res').click();
    });

    $('#HeaderImage .header_img_res').on('change',function(){
        var imgUrl = getObjectURL($(this)[0].files[0]);
        console.log($(this)[0].files[0]);
        if($(this)[0].files[0] > 2048000){
            return false;
        }

        if(!$(this)[0].files[0].type=='image/png'||!$(this)[0].files[0].type=='image/gif'||!$(this)[0].files[0].type=='image/jpeg'){
            return false;
        }

        $('.head_pic img').attr({
            src : imgUrl
        });

        $(this).parents('form').on_submit(function (e) {
            console.log(e)
        });
    });

    $('.table_box_item .user_info .input.radio label').click(function(){
        // 个人资料性别点击绑定选中状态
        $('.table_box_item .user_info .input.radio label').removeClass('active');
        $(this).addClass('active');
    });
    $('.teble_btn .teble_item').click(function(e){
        // 个人中心选项卡点击切换
        e.preventDefault();
        $('.teble_btn .teble_item').removeAttr('active');
        $(this).attr('active','');
        var index = $($(this).attr('href')).index('.table_box .table_box_item');
        $('#table_box .table_box').animate({
            marginLeft:'-'+(index * 1230)+'px'
        },200);
    });
    user_pic_page = new iPage({
        // 绑定分页
        elem: '#user_pic_page',
        limit: 5, //分页数
        curr: 1, //初始页
        count: 200, //数据总数,
        hidenum: 15,
        topages: true,
        jump: function (obj, first) {
            // console.log(obj);
        }
    });

    //
    $('.table_box_item .user_info .submit').unbind('click'); //修改资料保存按钮默认事件
    $('.table_box_item .user_info .submit').click(function (event) { //点击保存按钮
        $(this).parents('form').on_submit(function (e) {
            console.log(e);
        });
    });


    $('#pic_upload .pic_upload .tops .item span').click(function(){
        //点击多图单图的切换按钮
        $('#pic_upload .pic_upload .tops .item span').removeClass('active');
        $(this).addClass('active');
        $('#pic_upload .pic_upload .tops .item').attr('type',$(this).attr('type'));
        if($(this).attr('type') == 'multi'){//设置上传控件选择多图和单图
            $('.pic_upload_files').attr('multiple','true');
        }else{
            $('.pic_upload_files').removeAttr('multiple');
        }
    });

    $('#pic_upload_single .upload_btn').click(function(){
        $('.pic_upload_files').click();//绑定点击上传按钮到悬着文件按钮
    });

    var pic_upload_single = document.getElementById('pic_upload_single');

    pic_upload_single.ondragenter = function(e){
        e.preventDefault();
    }
    pic_upload_single.ondragover = function(e){
        e.preventDefault();
        $(this).addClass('ondragover');
    }
    pic_upload_single.ondragleave = function(e){
        e.preventDefault();
        $(this).removeClass('ondragover')
    }
    pic_upload_single.ondrop = function(e){
        e.preventDefault();
        $(this).removeClass('ondragover')
        window.dropFiles = window.dropFiles || []; //获取要上传的文件对象(可以上传多个) 
        if(!$('.pic_upload_files').attr('multiple')){
            $('#pic_upload_single .imglist').html(''    );
            if(e.dataTransfer.files[0].type=='image/png'||e.dataTransfer.files[0].type=='image/gif'||e.dataTransfer.files[0].type=='image/jpeg'){
                dropFiles = [e.dataTransfer.files[0]];
                addendUploadPic(getObjectURL(e.dataTransfer.files[0]));
                isUploadPic();
                return;
            }else{
                console.log('拖动的文件不是支持的图片格式。');
                isUploadPic();
                return;
            }

        }
        for(var i = 0 ; i < e.dataTransfer.files.length;i++){
            if(e.dataTransfer.files[i].type=='image/png'||e.dataTransfer.files[i].type=='image/gif'||e.dataTransfer.files[i].type=='image/jpeg'){
                addendUploadPic(getObjectURL(e.dataTransfer.files[i]));
                dropFiles.push(e.dataTransfer.files[i]);
                console.log(dropFiles);
            }else{
                console.log('拖动的文件不是支持的图片格式。');
                isUploadPic();
                return;
            }
        }
        isUploadPic();
    }

    $('.pic_upload_files').change(function(){
        console.log($(this)[0].files);
        window.dropFiles = window.dropFiles || [];
        $(this).parents('.button').addClass('before');
        if(!$('.pic_upload_files').attr('multiple')){
            if($(this)[0].files[0].type=='image/png'||$(this)[0].files[0].type=='image/gif'||$(this)[0].files[0].type=='image/jpeg'){
                $('#pic_upload_single .imglist').html('');
                addendUploadPic(getObjectURL($(this)[0].files[0]));
                dropFiles = [$(this)[0].files[0]];
                isUploadPic();
                return
            }else{
                console.log('上传的文件不是支持的图片格式。');
                isUploadPic();
                return;
            }
        }
        for(var i = 0 ; i < $(this)[0].files.length;i++){
            if($(this)[0].files[i].type=='image/png'||$(this)[0].files[i].type=='image/gif'||$(this)[0].files[i].type=='image/jpeg'){
                addendUploadPic(getObjectURL($(this)[0].files[i]));
                dropFiles.push($(this)[0].files[i]);
            }else{
                console.log('上传的文件不是支持的图片格式。');
                isUploadPic();
                return;
            }

        }
    });
    $('#pic_form .submit').unbind('click'); //修改资料保存按钮默认事件
    $('#pic_form .submit').click(function (event) { //点击保存按钮
        var _this = $(this).parents('form');
        if(_this.attr('method') == 'post'){
            var data = new FormData(_this[0])
        }else{
            console.error('上传图片请用post方式上传');
            return;
        }

        // 表单数据验证
        if(!$('.pic_upload_files').attr('multiple')){
            console.log('单选');
        }else{
            console.log('多选');
        }
        var isdata = unserializeArray(_this.serializeArray());
        if(typeof(dropFiles) == 'undefined'){
            window.dropFiles = [];
        }
        console.log(isdata);
        if(dropFiles.length < 1){
            console.log('没有选择图片');
            return;
        }
        if(!isdata.pic_class){
            console.log('没有选择图片类型');
            return;
        }
        if(!isdata.type){
            console.log('没有选择图片版权');
            return;
        }
        if(!isdata.pid){
            console.log('请选择至少一项图片分类');
            return;
        }
        if(!isdata.title){
            console.log('请输入图片标题');
            return;
        }

        dropFiles.forEach(function(item,index){
            data.append('files',item);
        });
        $.ajax({
            url: _this.attr('action'),
            type: _this.attr('method').toUpperCase(),
            cache: false,
            data: data,
            processData: false,
            contentType: false,
            dataType: "json",
            xhr: function(){ //获取ajaxSettings中的xhr对象，为它的upload属性绑定progress事件的处理函数    
                myXhr = $.ajaxSettings.xhr();
                if(myXhr.upload){ //检查upload属性是否存在    
                    myXhr.upload.addEventListener('progress',progressHandlingFunction, false);
                }
                return myXhr; //xhr对象返回给jQuery使用    
            } ,
            beforeSend: function (res) {
            },
            success: function (res) {
                console.log(res);
                if(res.code == 1){
                    location.reload();
                }
            }
        });
        function progressHandlingFunction(e){
            var curr=e.loaded;
            var total=e.total;
            process=curr/total*100;
            console.log(process);
        }
    });

    $('#pic_upload_single .imglist').on('click','.img',function(){//点击图片删除
        var index = $(this).index('#pic_upload_single .imglist .img');
        $(this).remove();
        dropFiles.splice(index, 1);
    });
    var diy_tag_list = [];
    $('#pic_form .set_tags').click(function(){
        var inputTag = $.trim($('#pic_form .input_tags').val());
        if(inputTag == ''){
            return;
        }
        var iscf = 0;
        diy_tag_list.forEach(function(item){
            if(item == inputTag){
                iscf = 1;
            }
        });
        if(iscf){console.log('重复标签');return;}
        diy_tag_list.push(inputTag);
        $('#pic_form .diy_tags').val(diy_tag_list.join(','));
        $('#pic_form .input_tags').val('');
        $('#pic_form .tag_list').append('<span class="item">'+inputTag+'<i></i></span>');
    });
    $('#pic_form .input_tags').keydown(function(event){
        if(event.keyCode == "13") {
            var inputTag = $.trim($('#pic_form .input_tags').val());
            if(inputTag == ''){
                return;
            }
            var iscf = 0;
            diy_tag_list.forEach(function(item){
                if(item == inputTag){
                    iscf = 1;
                }
            });
            if(iscf){console.log('重复标签');return;}
            diy_tag_list.push(inputTag);
            $('#pic_form .diy_tags').val(diy_tag_list.join(','));
            $('#pic_form .input_tags').val('');
            $('#pic_form .tag_list').append('<span class="item">'+inputTag+'<i></i></span>');
        }
    });
    $('#pic_upload #pic_form .input .input_item .tag_list').on('click','span',function(){
        var indexs;
        var _this = $(this);
        diy_tag_list.forEach(function(item,index){
            if(item == _this.text()){
                indexs = index;
            }
        });
        diy_tag_list.splice(indexs,1);
        console.log(diy_tag_list);
        $('#pic_form .diy_tags').val(diy_tag_list.join(','));
        _this.remove();
    });

    $('#pic_upload #pic_form .select label').click(function(){
        var that = $(this).parents('.select');
        if(that.hasClass('radio')){
            that.find('label').removeClass('active');
            $(this).addClass('active');
        }else{
            $(this).toggleClass('active');
        }
    });
    $('#pic_form input[type="checkbox"]').click(function(event){
        event.stopPropagation();
    });
    $('#user_box .user_item .option .pic').click(function(){
        $('#pic_upload').fadeIn();
    });
    $('#pic_upload').click(function(){
        $('#pic_upload').fadeOut();
    });
    $('#pic_upload .pic_upload').click(function(event){
        event.stopPropagation();
    });
})